package org.toves.checklua;

import java.util.Scanner;

import org.toves.checklua.ast.AstNode;
import org.toves.checklua.ast.Complaint;
import org.toves.checklua.scan.Token;

public class InteractiveTest {
	public static void main(String[] args) {
		Checker checker = new Checker();
		Scanner in = new Scanner(System.in);
		while (true) {
			System.out.print("> ");
			if (!in.hasNextLine()) break;
			String line = in.nextLine();
			
			CheckerResult result = checker.process(line);
			for (Complaint c : result.getComplaints()) {
				System.out.println(c.toString());
			}
			if (!result.containsError()) {
				result.commit();
				System.out.println("ok");
			}
		}
	}
	
	public static void printTree(AstNode root, String prefix) {
		Token loc = root.getRepresentativeToken();
		String locText;
		if (loc == null) {
			locText = "(null)";
		} else {
			locText = loc.getLine() + "." + loc.getColumn()
				+ " '" + loc.getText(10) + "'";
		}
		String line = prefix + root.getNodeType().toString() + " / " + locText;
		System.out.println(line);
		prefix += "  ";
		for (Object parm : root.getParameters()) {
			if (parm instanceof AstNode) {
				printTree((AstNode) parm, prefix);
			} else if (parm instanceof AstNode[]) {
				System.out.println(prefix + "[");
				for (AstNode p : (AstNode[]) parm) {
					printTree(p, prefix + "  ");
				}
				System.out.println(prefix + "]");
			} else if (parm == null) {
				System.out.println(prefix + "(null)");
			} else {
				System.out.println(prefix + parm.toString());
			}
		}
	}
}
